/*
package com.dailyblue.java.spring.boot.shopping.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONReader;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dailyblue.java.spring.boot.commons.bean.JsonResult;
import com.dailyblue.java.spring.boot.commons.bean.ResultTool;
import com.dailyblue.java.spring.boot.shopping.bean.Goods;
import com.dailyblue.java.spring.boot.shopping.mapper.GoodsMapper;
import com.dailyblue.java.spring.boot.shopping.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

//@Service
@Slf4j
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {
    @Resource
    private GoodsMapper goodsMapper;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    private ValueOperations<String, String> value;


    //@Cacheable(cacheNames = "GOODS_ALL", key = "0")
    public JsonResult findAll() {
        if (value == null) {
            value = stringRedisTemplate.opsForValue();
        }
        log.info("------------------->");
        log.info("开始获取findAll信息");
        String s = value.get("GOODS_ALL::0");
        log.info("获取到的信息是：{}", s);
        if (s == null) {
            JsonResult json = ResultTool.success(goodsMapper.findAllGoods());
            log.info("s是空的，从数据库获取:{}", json);
            value.set("GOODS_ALL::0", JSONArray.toJSONString(json));
            return json;
        }
        JsonResult result = JSONArray.parseObject(s, JsonResult.class);
        log.info("不是空的内容是：{}", result);
        return result;
    }

    //@Cacheable(cacheNames = "GOODS_ID", key = "#id")
    public JsonResult findById(int id) {
        if (value == null) {
            value = stringRedisTemplate.opsForValue();
        }
        String s = value.get("GOODS_ID::" + id);
        if (s == null) {
            JsonResult json = ResultTool.success(goodsMapper.selectById(id));
            value.set("GOODS_ID::" + id, JSONArray.toJSONString(json));
            return json;
        }
        log.info("s的内容是：{}", s);
        return JSONArray.parseObject(s, JsonResult.class);
    }

    @Override
    public JsonResult add(int id) {
        value = stringRedisTemplate.opsForValue();
        log.info("将{}商品加入购物车", id);
        // 1.查询id对应的商品信息
        JsonResult<Goods> goods = findById(id);
        log.info("第1步：查询{}对应的商品是:{}", id, goods);
        // 2.将这个商品加入到购物车篮子中
        List<Goods> gwcList;
        // 是第一次加入购物车还是非第一次
        String result = value.get("GWC_LIST");
        log.info("判断redis中是否存放了GWC_LIST?{}", result);
        if (result == null) {
            log.info("没有购物车，新建一个购物车");
            // 第一次加入购物车，需要产生购物车的集合
            gwcList = new ArrayList<>();
        } else {
            log.info("有购物车，获取到原有购物车");
            // 获取到原来的购物车
            gwcList = JSONArray.parseArray(result, Goods.class);
            log.info("原来的购物车是：{}", gwcList);
        }
        // 把这个商品加入到购物车中
        gwcList.add(goods.getData());
        value.set("GWC_LIST", JSONArray.toJSONString(gwcList));
        // 3.更新这个商品的库存信息
        // 获取到所有商品信息
        String goods_result = value.get("GOODS_ALL::0");
        log.info("获取原来商品信息:{}", goods_result);
        JsonResult<List> goods_json = JSONArray.parseObject(goods_result, JsonResult.class);
        log.info("JSONResult格式内容是：{}", goods_json);
        List goodsList = goods_json.getData();
        log.info("集合数据是:{}", goodsList);
        int i = 0;
        for (Object g : goodsList) {
            Goods g1 = JSONArray.parseObject(g.toString(), Goods.class);
            log.info("检查{}和{}之间的关系", g1, id);
            if (id == g1.getId()) {
                g1.setNum(g1.getNum() - 1);
                goodsList.set(i, JSONArray.toJSONString(g1));
                break;
            }
            i++;
        }
        log.info("新的集合内容是：{}", goodsList);
        // 重新存放到redis中
        JsonResult jj = ResultTool.success(goodsList);
        value.set("GOODS_ALL::0", JSONArray.toJSONString(jj));
        return jj;
    }

    public JsonResult add2(int id) {
        if (value == null) {
            value = stringRedisTemplate.opsForValue();
        }
        // 1. 获取到id对应的商品
        JsonResult goods_json = findById(id);
        log.info("goods_json:{}", goods_json);
        Goods goods = JSONArray.parseObject(goods_json.getData().toString(), Goods.class);
        // 2. 将这个商品加入购物车
        List<Goods> gwcList;
        // 判断redis中是否存在gwcList
        String r1 = value.get("GWC_LIST");
        if (r1 == null) {
            gwcList = new ArrayList<>();
        } else {
            gwcList = JSONArray.parseArray(r1, Goods.class);
        }
        // 将商品放入购物车集合
        gwcList.add(goods);
        // 把集合存放到redis中
        value.set("GWC_LIST", JSONArray.toJSONString(gwcList));
        // 3. 更新商品集合信息
        // 所有商品信息获取到了
        JsonResult<List> r2 = findAll();
        log.info("{}", r2);
        log.info("{}", r2.getData());
        List obj = r2.getData();
        List<Goods> beforeGoods = new ArrayList<>();
        // 把库存-1
        for (int i = 0; i < obj.size(); i++) {
            Object oo = obj.get(i);
            log.info("oo:{}", oo);
            Goods g = JSONArray.parseObject(oo.toString(), Goods.class);
            if (g.getId() == id) {
                g.setNum(g.getNum() - 1);
            }
            beforeGoods.add(g);
        }
        value.set("GOODS_ALL::0", JSONArray.toJSONString(ResultTool.success(beforeGoods)));
        return ResultTool.success(beforeGoods);
    }
}*/
